#include "playscene.h"
#include <QDebug>
#include <QPainter>
#include <QMenuBar>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include <QPropertyAnimation>
#include "dataconfig.h"
#include "QSound"

PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}

PlayScene::PlayScene(int level)
{
    this->levelIndex = level;

    QString str = QString("进入了第 %1 关").arg(level);
    qDebug() << str;


    //设置固定大小
    setFixedSize(320,588);

    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));

    setWindowTitle("游戏场景");

    //创建菜单栏
    QMenuBar * bar = new QMenuBar();
    setMenuBar(bar);

    QMenu * startMenu = bar->addMenu("开始");
    QAction * quitActionn = startMenu->addAction("退出");

    connect(quitActionn, &QAction::triggered, [=](){
        this->close();
    });

    dataConfig coinData;
    //初始化gameArray
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            gameArray[i][j] = coinData.mData[levelIndex][i][j];
        }

    }

     QSound * leaveSound = new QSound(":/res/LevelWinSound.wav",this);
     QSound * coinSound = new QSound(":/res/ConFlipSound.wav",this);
     QSound * clickSound = new QSound(":/res/TapButtonSound.wav",this);

    //胜利图片显示
    QLabel *winLabel = new QLabel(this);
    QPixmap pix;
    pix.load(":/res/LevelCompletedDialogBg.png");
    //winLabel->setFixedSize(pix.width(), pix.height());
    winLabel->setGeometry(0, 0, pix.width(), pix.height());
    winLabel->move((this->width()-pix.width())*0.5, -pix.height());
    winLabel->setPixmap(pix);



    //金币背景
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            QPixmap pix;
            pix.load(":/res/BoardNode.png");
            QLabel* label = new QLabel(this);
            label->setGeometry(0,0, pix.width(), pix.height());
            label->setPixmap(pix);
            label->move(57+i*50, 200+j*50);

            QString str;
            bool bFlag;
            if (gameArray[i][j] == 1)
            {
                str = ":/res/Coin0001.png";
                bFlag = true;
            }
            else
            {
                str = ":/res/Coin0008.png";
                bFlag = false;
            }
            MyCoinBtn * coin = new MyCoinBtn(str);
            //coin->setGeometry(0,0, pix.width(), pix.height());
            coin->move(59+i*50, 204+j*50);
            coin->setParent(this);
            coin->posX = i;
            coin->posY = j;
            coin->bFlag = bFlag;

            coinBtnArray[i][j] = coin;

            //点击金币，翻转
            connect(coin, &MyCoinBtn::clicked, [=](){

                coinSound->play();

                coin->changeFlag();
                this->gameArray[coin->posX][coin->posY] = gameArray[coin->posX][coin->posY]==0 ? 1 : 0;

                //异步延时，注意
                QTimer::singleShot(200, this, [=](){
                    //翻转周围
                    if (coin->posX < 3) //右侧
                    {
                        coinBtnArray[coin->posX+1][coin->posY]->changeFlag();
                        gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]==0 ? 1 : 0;

                    }

                    if (coin->posX > 0) //左侧
                    {
                        coinBtnArray[coin->posX-1][coin->posY]->changeFlag();
                        gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]==0 ? 1 : 0;

                    }

                    if (coin->posY < 3) //下侧
                    {
                        coinBtnArray[coin->posX][coin->posY+1]->changeFlag();
                        gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX][coin->posY+1]==0 ? 1 : 0;

                    }

                    if (coin->posY > 0) //上侧
                    {
                        coinBtnArray[coin->posX][coin->posY-1]->changeFlag();
                        gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1]==0 ? 1 : 0;

                    }

                    isWin = true;
                    for (int i = 0;i < 4; ++i)
                    {
                        for (int j = 0; j < 4; ++j)
                        {
                            if (gameArray[i][j] == 0)
                            {
                                isWin = false;
                                //qDebug() << QString("%1 %2").arg(i).arg(j);


                            }
                        }
                    }
                    if (isWin)
                    {
                        //禁用
                        for (int i = 0;i < 4; ++i)
                        {
                            for (int j = 0; j < 4; ++j)
                            {
                                coinBtnArray[i][j]->isWin = true;
                            }

                        }

                        leaveSound->play();

                        //胜利图片
                        QPropertyAnimation *animation = new QPropertyAnimation(winLabel, "geometry");
                        animation->setDuration(200);
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y()+114, winLabel->width(), winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();

                        //异步延时，注意
                       QTimer::singleShot(1000, this, [=](){
                           //進入下一關
                           emit this->nextScene(this->levelIndex);


                       });

                    }


                });
            });

        }
    }


    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(), this->height()-backBtn->height());


    connect(backBtn, &MyPushButton::clicked, [=](){
         clickSound->play();

        QTimer::singleShot(500, this, [=](){
        //自定义信号
            emit this->chooseSceneBack();
        });

    });

    //显示当前关卡数
    QLabel * label = new QLabel(this);
    label->setText(QString("level: %1").arg(this->levelIndex));
    label->setGeometry(30, this->height()-50, 130, 50);

    QFont font;
    font.setFamily("华为新魏");
    font.setPointSize(20);
    label->setFont(font);




}

void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0,this->width(), this->height(), pix);

    //图标
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5, pix.height()*0.5);
    painter.drawPixmap(10, 30, pix);
}
